<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      :error.sync="error"
      finished-text="没有更多了"
      error-text="获取失败，请点击刷新"
      @load="onLoad"
    >
    </van-list>
  </div>
</template>

<script>
import { searchResultData } from '@/api/search'
export default {
  name: 'SearchResult',
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      error: false,
      page: 1
    }
  },
  props: {
    searchText: {
      type: String
    }
  },
  created() {
    console.log(this.searchText)
  },
  methods: {
    async onLoad() {
      console.log(this.searchText, 'this.searchText')
      try {
        // 异步更新数据
        const { data } = await searchResultData({
          page: this.page,
          per_page: 20,
          q: this.searchText
        })
        this.page++
        console.log(data)
        this.list.push(...data.results)
        // setTimeout 仅做示例，真实场景中一般为 ajax 请求
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (!data.results.length) {
          this.finished = true
        }
      } catch (error) {
        this.error = true
        this.$toast('获取失败，请点击刷新')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
